﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Chapter1
{
    public struct COPYDATASTRUCT
    {
        public IntPtr dwData;
        public int cbData;
        [MarshalAs(UnmanagedType.LPStr)]
        public string lpData;
    }
    public partial class Form1 : Form
    {
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr wnd, int msg, IntPtr wP, ref COPYDATASTRUCT lParam);


        const int WM_COPYDATA = 0x004A;
        public static StringBuilder cmdOutput = null;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Text = "";
            Process process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            string address = textBox1.Text;
            string strCmd = "ping " + address + " -n 5";
            process.Start();
            process.StandardInput.WriteLine(strCmd);
            process.StandardInput.WriteLine("exit");
            textBox2.Text = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            process.Close();
        }
        private void cmdOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
        {
            cmdOutput.AppendLine(outLine.Data);
            //通过FindWindow API的方式找到目标进程句柄，然后发送消息
            IntPtr WINDOW_HANDLER = FindWindow(null, "Form1");
            if (WINDOW_HANDLER != IntPtr.Zero)
            {
                COPYDATASTRUCT mystr = new COPYDATASTRUCT();
                mystr.dwData = (IntPtr)0;
                string ii = outLine.Data;
                if (ii == null) return;
                if (ii.Equals(""))
                {
                    byte[] sarr = System.Text.Encoding.Unicode.GetBytes(" ");
                    mystr.cbData = sarr.Length + 1;
                    mystr.lpData = outLine.Data;
                }
                else
                {
                    byte[] sarr = System.Text.Encoding.Unicode.GetBytes(outLine.Data);
                    mystr.cbData = sarr.Length + 1;
                    mystr.lpData = outLine.Data;
                }
                SendMessage(WINDOW_HANDLER, WM_COPYDATA, (IntPtr)0, ref mystr);
            }
        }
        protected override void DefWndProc(ref System.Windows.Forms.Message m)
        {
            switch (m.Msg)
            {
                case WM_COPYDATA:
                    COPYDATASTRUCT mystr = new COPYDATASTRUCT();
                    Type mytype = mystr.GetType();
                    mystr = (COPYDATASTRUCT)m.GetLParam(mytype);
                    this.textBox2.Text += mystr.lpData + "\r\n";
                    break;
                default:
                    base.DefWndProc(ref m);
                    break;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            textBox2.Text = "";
            cmdOutput = new StringBuilder();
            Process process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.OutputDataReceived += new DataReceivedEventHandler(cmdOutputHandler);
            //重定向输出到自定义对象
            string address = textBox1.Text;
            string strCmd = "ping " + address + " -n 5";
            process.Start();
            process.StandardInput.WriteLine(strCmd);
            process.StandardInput.WriteLine("exit");
            process.BeginOutputReadLine();
        }
    }
}
